package api

import (
	"gitee.com/shiwjlinux/arm/apps/host"
	"gitee.com/shiwjlinux/arm/common"
	"gitee.com/shiwjlinux/arm/response"
	"github.com/gin-gonic/gin"
)

func (h *ApiHandler) Registry(appRouter gin.IRouter) {
	appRouter.GET("/", h.QueryHost)
	appRouter.GET("/:id", h.DescribeHost)

	appRouter.POST("/", h.CreateHost)
	appRouter.PUT("/:id", h.UpdateHost)
	appRouter.DELETE("/:id", h.DeleteHost)
}

func (h *ApiHandler) QueryHost(ctx *gin.Context) {
	req := host.NewQueryHostRequest()
	pageRequest := common.NewPageRequest()
	req.PageRequest = pageRequest.PageRequestFromGinCtx(ctx)

	req.Hostname = ctx.Query("hostname")
	req.Keywords = ctx.Query("keywords")
	req.Sn = ctx.Query("sn")

	set, err := h.svc.QueryHost(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, set)
}

func (h *ApiHandler) DescribeHost(ctx *gin.Context) {
	req := host.NewDescribeHostRequest(ctx.Param("id"))
	ins, err := h.svc.DescribeHost(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, ins)
}

func (h *ApiHandler) CreateHost(ctx *gin.Context) {
	req := host.NewCreateHostRequest("", "")
	if err := ctx.Bind(req); err != nil {
		response.Failed(ctx, err)
		return
	}
	ins, err := h.svc.CreateHost(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, ins)
}
func (h *ApiHandler) UpdateHost(ctx *gin.Context) {
	req := host.NewUpdateHostRequestById(ctx.Param("id"))
	if err := ctx.Bind(req.CreateHostRequest); err != nil {
		response.Failed(ctx, err)
		return
	}
	// h.logger.Debug().Msgf("UpdateHost %+v", req)
	ins, err := h.svc.UpdateHost(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
	}
	response.Success(ctx, ins)
}
func (h *ApiHandler) DeleteHost(*gin.Context) {}
